package com.robot.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.robot.common.enums.IsDeleteEnum;
import com.robot.common.result.PageResult;
import com.robot.dao.entity.Robot;
import com.robot.dao.entity.RobotUserUsetime;
import com.robot.dao.mapper.RobotMapper;
import com.robot.dao.mapper.RobotUserUsetimeMapper;
import com.robot.dao.vo.RobotOverViewVO;
import com.robot.repository.RobotRepoEntity;
import com.robot.repository.RobotRepository;
import com.robot.service.RobotUserUsetimeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Function;

@Service
public class RobotUserUsetimeServiceImpl extends ServiceImpl<RobotUserUsetimeMapper, RobotUserUsetime> implements RobotUserUsetimeService {

    @Autowired
    private RobotUserUsetimeMapper robotUserUsetimeMapper;
    @Autowired
    private RobotRepository robotRepository;

    @Override
    public void creatrobotuser(String robotId, String userId) {
        RobotUserUsetime robotUserUsetime = robotUserUsetimeMapper.queryByUseridAndRobotid(Long.valueOf(userId),Long.valueOf(robotId));
        if (robotUserUsetime==null){
            RobotUserUsetime robotUserUsetimetmp = new RobotUserUsetime();
            robotUserUsetimetmp.setRobotId(Long.valueOf(robotId));
            robotUserUsetimetmp.setUserId(Long.valueOf(userId));
            robotUserUsetimetmp.setIsDelete(Long.valueOf(IsDeleteEnum.EXIST.getValue()));
            robotUserUsetimetmp.setLastUsetime(LocalDateTime.now());
            this.save(robotUserUsetimetmp);
        }else {
            robotUserUsetime.setLastUsetime(LocalDateTime.now());
            this.updateById(robotUserUsetime);
        }
    }

    @Override
    public void deleterobotuser(String robotId, String userId) {
        RobotUserUsetime robotUserUsetime = robotUserUsetimeMapper.queryByUseridAndRobotid(Long.valueOf(userId),Long.valueOf(robotId));
        if (robotUserUsetime!= null){
            robotUserUsetime.setIsDelete(Long.valueOf(IsDeleteEnum.DELETED.getValue()));
            this.updateById(robotUserUsetime);
        }
    }

    @Override
    public void deleterobot(String robotId) {
        List<RobotUserUsetime> robotUserUsetimeList = robotUserUsetimeMapper.queryByRobotId(Long.valueOf(robotId));
        if (!robotUserUsetimeList.isEmpty()){
            for (RobotUserUsetime robotUserUsetimetmp : robotUserUsetimeList) {
                robotUserUsetimetmp.setIsDelete(Long.valueOf(IsDeleteEnum.DELETED.getValue()));
                this.updateById(robotUserUsetimetmp);
            }
        }
    }

    @Override
    public PageResult pagequerybyuserid(String userId, int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        Page<RobotUserUsetime> pageResult = robotUserUsetimeMapper.queryByUserid(Long.valueOf(userId));

        List<RobotUserUsetime> robotUser = pageResult.getResult();
        List<RobotOverViewVO> robotList = new ArrayList<>();
        for (RobotUserUsetime robotuser : robotUser) {
            RobotRepoEntity robot = robotRepository.queryByRobotId(robotuser.getRobotId());
            RobotOverViewVO robotVO = new RobotOverViewVO();
            BeanUtils.copyProperties(robot, robotVO);
            robotVO.setRobotId(String.valueOf(robot.getId()));
            robotList.add(robotVO);
        }
        boolean hasMore = page < pageResult.getPages();
        return new PageResult(pageResult.getTotal(), hasMore, robotList);
    }

}
